<?php
	if ($mod=="jobs"){
		if ($act == 'rename') {
			if (isset($_POST["name"]) && (strlen($_POST["name"])> 0)){
				if (isset($_POST["group_id"]) && (strlen($_POST["group_id"])> 0)){
					$source_type_html = ""; $source_type_text = "";
					if (isset($_POST["source_type_html"]) && (strlen($_POST["source_type_html"])> 0)) $source_type_html = $_POST["source_type_html"];
					//if (isset($_POST["source_type_text"]) && (strlen($_POST["source_type_text"])> 0)) $source_type_text = $_POST["source_type_text"];
					$lettersclass->jobs_rename($_POST["name"], $_POST["group_id"], $source_type_html, $source_type_text);
				}
			} else $err->AddError(5);
		} elseif ($act == 'add') {
			if (isset($_POST["name"]) && (strlen($_POST["name"])> 0)){
				$source_type_html = ""; $source_type_text = "";
				if (isset($_POST["source_type_html"]) && (strlen($_POST["source_type_html"])> 0)) $source_type_html = $_POST["source_type_html"];
				if (isset($_POST["source_type_text"]) && (strlen($_POST["source_type_text"])> 0)) $source_type_text = $_POST["source_type_text"];
				$lettersclass->jobs_add($_POST["name"], $source_type_html, $source_type_text);
			} else $err->AddError(5);
		} elseif ($act == 'del_mas') {
			$del_mas = array();
			if (isset($_POST["ids"])) $del_mas=$_POST["ids"];
			if (count($del_mas) > 0){
				$lettersclass->jobs_del($del_mas);
			}
		} elseif ($act == 'mod') {
			$job_html = ""; $job_text = ""; $job_subject = ""; $job_from = "";
			$job_title = ""; $job_header = ""; $job_footer = "";
			if (isset($_POST["letters_text"]) && (strlen($_POST["letters_text"])> 0)) $job_text = $_POST["letters_text"];
			if (isset($_POST["letters_html"]) && (strlen($_POST["letters_html"])> 0)) $job_html = $_POST["letters_html"];
			if (isset($_POST["letters_subject"]) && (strlen($_POST["letters_subject"])> 0)) $job_subject = $_POST["letters_subject"];
			if (isset($_POST["letters_from"]) && (strlen($_POST["letters_from"])> 0)) $job_from = $_POST["letters_from"];
			if (isset($_POST["letters_title"]) && (strlen($_POST["letters_title"])> 0)) $job_title = $_POST["letters_title"];
			if (isset($_POST["letters_header"]) && (strlen($_POST["letters_header"])> 0)) $job_header = $_POST["letters_header"];
			if (isset($_POST["letters_footer"]) && (strlen($_POST["letters_footer"])> 0)) $job_footer = $_POST["letters_footer"];
			if (isset($_POST["letters_unsubscribe"]) && (strlen($_POST["letters_unsubscribe"])> 0)) $job_unsubscribe = $_POST["letters_unsubscribe"];
			$lettersclass->jobs_mod($parent, $job_html, $job_text, $job_subject, $job_from, $job_title, $job_header, $job_footer, $job_unsubscribe);
		} elseif ($act == 'import') {
			$job_source_type = ""; $job_source_name = "";
			if (isset($_POST["source_type"]) && (strlen($_POST["source_type"])> 0)) $job_source_type = $_POST["source_type"];
			if ($job_source_type == "files") $job_source_name = $_FILES['source_name'];
			elseif ($job_source_type == "textfield") $job_source_name = $_POST['address_list'];
			elseif ($job_source_type == "clients") {
				$del_mas = array();
				if (isset($_POST["ids"])) $del_mas=$_POST["ids"];
				$job_source_name = $del_mas;
			}
			$lettersclass->job_emails_import($parent, $job_source_type, $job_source_name);
		} elseif ($act == 'load') {
			$load = array();
			$job_id = 0;
			if (isset($_POST["job_id"]) && (strlen($_POST["job_id"]) > 0)) $job_id = $_POST["job_id"]; 
			if ($job_id == 0) if (isset($_GET["job_id"]) && (strlen($_GET["job_id"]) > 0)) $job_id = $_GET["job_id"];
			$load = $lettersclass->get_job_status($job_id);
			$smarty->assign('status_data', $load);
			$smarty->template_dir = $setupdata['smarty_admin_template_dir'];
			$smarty->display('admin/modules/letters/status.html');
			$mysqlclass->close_db();
			include $setupdata['core_dir']."/includes/die.php";
		} elseif ($act == 'go') {
			if (isset($_GET["resend"]) && (strlen($_GET["resend"]) > 0)) {
				$mysqlclass->mysql_my_query("UPDATE `".$setupdata['table_prefix']."letters` SET `date_end`='0000-00-00 00:00:00', `date_start`='0000-00-00 00:00:00'  WHERE ".$job_id."=`id`", __LINE__, __FILE__);
				$mysqlclass->mysql_my_query("UPDATE `".$setupdata['table_prefix']."letters_jobs` SET `status`=0 WHERE ".$job_id."=`parent`", __LINE__, __FILE__);				
			}
			$job_id = 0;
			if (isset($_POST["job_id"]) && (strlen($_POST["job_id"]) > 0)) $job_id = $_POST["job_id"]; 
			if ($job_id == 0) if (isset($_GET["job_id"]) && (strlen($_GET["job_id"]) > 0)) $job_id = $_GET["job_id"];
			$sql = $mysqlclass->mysql_my_query("SELECT * FROM `".$setupdata['table_prefix']."letters` WHERE ".$job_id."=`id` AND `date_end`='0000-00-00 00:00:00'", __LINE__, __FILE__);			
			if (mysql_num_rows($sql) > 0){
				$job_info = mysql_fetch_array($sql, MYSQL_ASSOC);
				$job_info['letters_subject'] = stripslashes($job_info['letters_subject']);
				$job_info['letters_title'] = stripslashes($job_info['letters_title']);
				$job_info['letters_header'] = stripslashes($job_info['letters_header']);
				$job_info['letters_footer'] = stripslashes($job_info['letters_footer']);
				$job_info['letters_unsubscribe'] = stripslashes($job_info['letters_unsubscribe']);
				$job_info['html'] = stripslashes($job_info['html']);
				$job_info['text'] = stripslashes($job_info['text']);

				if ($job_info['date_start'] == '0000-00-00 00:00:00') $sql = $mysqlclass->mysql_my_query("UPDATE `".$setupdata['table_prefix']."letters` SET `date_start`='".date("YmdHis")."' WHERE ".$job_id."=`id`", __LINE__, __FILE__);
				
				$last_resize_start=time();
				$status_tmp = "send";
				$tmp = $smarty->template_dir;
				$smarty->template_dir = "";

				$smarty->assign("data_site", $treeclass->_get_site_settings());
				$smarty->assign("_http_path", $setupdata['base_href']);
				$smarty->assign("letter_data", $job_info);
				
				if ((isset($setupdata['smarty_guest_mail_template_dir'])) && file_exists($setupdata['smarty_guest_mail_template_dir'])) $tempalate = $setupdata['smarty_guest_mail_template_dir']."/".$job_info['tempalate_html'];
				elseif (file_exists("templates/guest/newsletters")) $tempalate = "templates/guest/newsletters/".$job_info['tempalate_html'];
				else $tempalate = $job_info['tempalate_html'];
				if ($smarty->template_exists($tempalate)) {
					$output_html = $smarty->fetch($tempalate);
					// parsing html - files ID array
					// 
				} else $output_html = "";
				if (isset($job_info['tempalate_text']) && (strlen($job_info['tempalate_text'])>0)){
					if ((isset($setupdata['smarty_guest_mail_template_dir'])) && file_exists($setupdata['smarty_guest_mail_template_dir'])) $tempalate = $setupdata['smarty_guest_mail_template_dir']."/".$job_info['tempalate_text'];
					elseif (file_exists("templates/guest/newsletters")) $tempalate = "templates/guest/newsletters/".$job_info['tempalate_text'];
					else $tempalate = $job_info['tempalate_text'];
					if ($smarty->template_exists($tempalate)) $output_text = $smarty->fetch($tempalate);
					else $output_text = "";
				} else $output_text = $job_info['text'];
				$smarty->clear_assign("letter_data");
				$smarty->template_dir = $tmp;
				$mail_settings['form_subject'] = $job_info['letters_subject'];
				if (strlen($job_info['letters_from']) > 0) $mail_settings['form_from'] = $job_info['letters_from'];
				if (!isset($setupdata['exec_time'])) $setupdata['exec_time'] = 10;
				while (((time()-$last_resize_start) < $setupdata['exec_time']) && ($status_tmp == "send")) {	
					$sql = $mysqlclass->mysql_my_query("SELECT * FROM `".$setupdata['table_prefix']."letters_jobs` WHERE ".$job_id."=`parent` AND `status`=0 LIMIT 1", __LINE__, __FILE__);
					if (mysql_num_rows($sql) == 0) $status_tmp = "done";
					else $status_tmp = "send";
					if ($status_tmp != "done"){
						$row = mysql_fetch_array($sql, MYSQL_ASSOC);
						$mail_settings['form_to'] = $row['email'];
						if (isset($_GET['attachment']) && ($_GET['attachment'] == 'on')) $res = $lettersclass->send_form_to_mail($output_html, $output_text, $mail_settings, "in");
						else $res = $lettersclass->send_form_to_mail($output_html, $output_text, $mail_settings, "out");
						if ($res == 1) $mysqlclass->mysql_my_query("UPDATE `".$setupdata['table_prefix']."letters_jobs` SET `status`=1 WHERE `id`=".$row['id'], __LINE__, __FILE__);				
					} else {
						$mysqlclass->mysql_my_query("UPDATE `".$setupdata['table_prefix']."letters` SET `date_end`='".date("YmdHis")."' WHERE ".$job_id."=`id`", __LINE__, __FILE__);
					}
				}
			} else {
				$sql = $mysqlclass->mysql_my_query("SELECT * FROM `".$setupdata['table_prefix']."letters` WHERE ".$job_id."=`id` AND `date_end`!='0000-00-00 00:00:00'", __LINE__, __FILE__);			
				if (mysql_num_rows($sql) > 0) $status_tmp = "done";
				else $status_tmp = "no";
			}
			$smarty->assign('system', $status_tmp);
			$smarty->template_dir = $setupdata['smarty_admin_template_dir'];
			$smarty->display('admin/modules/letters/system.html');
			$mysqlclass->close_db();
			include $setupdata['core_dir']."/includes/die.php";
		} elseif ($act == 'preview') {
			$job_id = 0;
			if (isset($_POST["job_id"]) && (strlen($_POST["job_id"]) > 0)) $job_id = $_POST["job_id"]; 
			if ($job_id == 0) if (isset($_GET["job_id"]) && (strlen($_GET["job_id"]) > 0)) $job_id = $_GET["job_id"];
			$sql = $mysqlclass->mysql_my_query("SELECT * FROM `".$setupdata['table_prefix']."letters` WHERE ".$job_id."=`id`", __LINE__, __FILE__);			
			$tmp = $smarty->template_dir;
			$smarty->template_dir = "";
			
			if (mysql_num_rows($sql) > 0){
				$job_info = mysql_fetch_array($sql, MYSQL_ASSOC);
				$job_info['name'] = stripslashes($job_info['name']);
				$job_info['letters_subject'] = stripslashes($job_info['letters_subject']);
				$job_info['letters_title'] = stripslashes($job_info['letters_title']);
				$job_info['letters_header'] = stripslashes($job_info['letters_header']);
				$job_info['letters_footer'] = stripslashes($job_info['letters_footer']);
				$job_info['letters_unsubscribe'] = stripslashes($job_info['letters_unsubscribe']);
				$job_info['html'] = stripslashes($job_info['html']);
				$job_info['text'] = stripslashes($job_info['text']);

				$smarty->assign("_http_path", $setupdata['base_href']);
				$smarty->assign("letter_data", $job_info);
				$smarty->assign("data_site", $treeclass->_get_site_settings());
				
				if ((isset($setupdata['smarty_guest_mail_template_dir'])) && file_exists($setupdata['smarty_guest_mail_template_dir'])) $tempalate = $setupdata['smarty_guest_mail_template_dir']."/".$job_info['tempalate_html'];
				elseif (file_exists("templates/guest/newsletters")) $tempalate = "templates/guest/newsletters/".$job_info['tempalate_html'];
				else $tempalate = $job_info['tempalate_html'];
				if ($smarty->template_exists($tempalate)) $output_html = $smarty->fetch($tempalate);
				$smarty->clear_assign("letter_data");
				$smarty->template_dir = $tmp;
				if (isset($output_html)) print_r($output_html);
				else print_r('Template no exists!');
				die();
			}
		}
	} elseif ($mod=="blacklist"){
		if ($act == 'add') {
			if ($name != NULL){
				$res = $lettersclass->email_add_to_blacklist($name);
			} else $res = 13;
			if ($res != 0) $err->AddError($res);
		} elseif ($act == 'del') {
			$del_mas = array();
			if (isset($_POST["ids"])) $del_mas=$_POST["ids"];
			if (count($del_mas) > 0){
				$lettersclass->email_del_from_blacklist($del_mas);
			}
		}
	}
?>